﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using TM.QWeibo.SDK.Objects;

namespace TM.QWeibo.SDK.API
{
    /// <summary>
    /// 与帐户相关的接口实现
    /// </summary>
    public class User
        : RequestBase
    {
        /// <summary>
        /// 根据授权对象实例化
        /// </summary>
        /// <param name="oauth"></param>
        public User(OAuth oauth)
            : base(oauth)
        { }

        #region 获取自己的资料

        Action<ResponseDataBase<UserInfo>> Callback { get; set; }

        /// <summary>
        /// 采用默认API请求地址获取用户本人帐号信息
        /// </summary>
        public void GetProfile(Action<ResponseDataBase<UserInfo>> callback)
        {
            this.GetProfile("http://open.t.qq.com/api/user/info",callback);
        }

        /// <summary>
        /// 获取用户本人帐号信息
        /// </summary>
        /// <param name="requestUrl">API请求地址</param>
        /// <param name="callback"></param>
        public void GetProfile(string requestUrl, Action<ResponseDataBase<UserInfo>> callback)
        {
            this.Callback = callback;
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            GetData(requestUrl,parameters,GetProfileEnd);
        }
        /// <summary>
        /// 获取个人信息结束
        /// </summary>
        /// <param name="json"></param>
        private void GetProfileEnd(string json)
        {
            if(Callback!=null)
            {
                var obj = Util.JsonToObject<ResponseDataBase<UserInfo>>(json);
                Callback(obj);
            }
        }
        #endregion

        #region 更新用户头像信息

        private Action<ResponseDataBase<string>> UpdateHeadCallback { get; set; }

        /// <summary>
        /// 采用默认API请求地址更新用户头像信息
        /// </summary>
        /// <param name="headPicture">头像图片文件</param>
        /// <param name="callback"></param>
        /// <returns>更新结果</returns>
        public void UpdateHead(UploadFile headPicture,Action<ResponseDataBase<string>> callback)
        {
            this.UpdateHeadCallback = callback;
            this.UpdateHead("http://open.t.qq.com/api/user/update_head", headPicture);
        }
        /// <summary>
        /// 更新用户头像信息
        /// </summary>
        /// <param name="requestUrl">API请求地址</param>
        /// <param name="headPicture">头像图片文件</param>
        /// <returns>更新结果</returns>
        private void UpdateHead(string requestUrl, UploadFile headPicture)
        {
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());

            var files = new Files();
            files.Add("pic", headPicture);
            this.PostData(requestUrl,parameters,files,UpdateHeadEnd);
        }

        /// <summary>
        /// 更新头像结束
        /// </summary>
        /// <param name="json"></param>
        private  void UpdateHeadEnd(string json)
        {
            if(UpdateHeadCallback!=null)
            {
                UpdateHeadCallback(Util.JsonToObject<ResponseDataBase<string>>(json));
            }
        }

        #endregion

        #region 获取其他人资料

        /// <summary>
        /// 采用默认API请求地址获取其他人资料
        /// </summary>
        /// <param name="name">他人的帐户名</param>
        /// <param name="callback"></param>
        public void GetOtherProfile(string name, Action<ResponseDataBase<UserInfo>> callback)
        {
            this.Callback = callback;
            this.GetOtherProfile("http://open.t.qq.com/api/user/other_info", name);
        }
        /// <summary>
        /// 获取其他人资料
        /// </summary>
        /// <param name="requestUrl">API请求地址</param>
        /// <param name="name">他人的帐户名</param>
        /// <returns>帐号信息.</returns>
        private void GetOtherProfile(string requestUrl, string name)
        {
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("name", name);
            GetData(requestUrl,parameters,GetProfileEnd);
        }

        #endregion

        #region 搜索用户

        /// <summary>
        /// 搜索用户
        /// </summary>
        /// <param name="keyword">搜索关键字</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="pageNum">页码</param>
        /// <param name="callback">回调</param>
        public void Search(string keyword, int pageSize, int pageNum, Action<ICollection<SDK.Objects.UserInfo>> callback)
        {
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("keyword", keyword);
            parameters.Add("pageSize", pageSize.ToString());
            parameters.Add("pageNum", pageNum.ToString());
            GetData("http://open.t.qq.com/api/search/user", parameters, (jsonStr) => {
                var obj = Util.JsonToObject<ResponseData<UserInfo>>(jsonStr);
                callback((obj!=null&&obj.Data!=null&&obj.Data.Info!=null)?obj.Data.Info:new List<SDK.Objects.UserInfo>());
            });      
        }

        /// <summary>
        /// 搜索用户
        /// </summary>
        /// <param name="keyword">搜索关键字</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="pageNum">页码</param>
        /// <param name="callback">回调</param>
        public void Search(string keyword, int pageSize, int pageNum, Action<ResponseDataObject<SDK.Objects.UserInfo>> callback)
        {
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("keyword", keyword);
            parameters.Add("pageSize", pageSize.ToString());
            parameters.Add("pageNum", pageNum.ToString());
            GetData("http://open.t.qq.com/api/search/user", parameters, (jsonStr) =>
            {
                var obj = Util.JsonToObject<ResponseData<UserInfo>>(jsonStr);
                callback((obj != null && obj.Data != null) ? obj.Data : new ResponseDataObject<SDK.Objects.UserInfo>());
            });
        }

        #endregion

        /// <summary>
        /// 获取一批人的简单资料
        /// </summary>
        /// <param name="names"> 用户id列表，用逗号“,”隔开，如 abc,edf,xxxx（可选）</param>
        /// <param name="callback"></param>
        public void GetBriefInfos(string names, Action<ICollection<SDK.Objects.UserInfo>> callback)
        {
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("names", names);
            GetData("http://open.t.qq.com/api/user/infos", parameters, (jsonStr) =>
            {
                var obj = Util.JsonToObject<ResponseData<UserInfo>>(jsonStr);
                callback((obj != null && obj.Data != null && obj.Data.Info != null) ? obj.Data.Info : new List<SDK.Objects.UserInfo>());
            });  
        }

        /// <summary>
        /// 拉黑
        /// </summary>
        /// <param name="name"></param>
        /// <param name="callback"></param>
        public void AddBlackList(string name,Action<bool> callback)
        {
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("name", name);
            PostData("http://open.t.qq.com/api/friends/addblacklist", parameters, (jsonStr) =>
            {
                var obj = Util.JsonToObject<ResponseData<string>>(jsonStr);
                var rs = obj.Ret == 0;
                callback(rs);
            }); 
        }

        /// <summary>
        /// 拉白
        /// </summary>
        /// <param name="name"></param>
        /// <param name="callback"></param>
        public void DeleteBlackList(string name, Action<bool> callback)
        {
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("name", name);
            PostData("http://open.t.qq.com/api/friends/delblacklist", parameters, (jsonStr) =>
            {
                var obj = Util.JsonToObject<ResponseData<string>>(jsonStr);
                var rs = obj.Ret == 0;
                callback(rs);
            });
        }
    }
}
